package v3.chapter3.s5;

import java.util.Date;

public class Run {

	public static void main(String[] args) {
		try {
			ThreadA a = new ThreadA();
			a.start();

			Thread.sleep(1000);

			ThreadB b = new ThreadB();
			b.start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadB extends Thread {

	@Override
	public void run() {
		try {
			for (int i = 0; i < 20; i++) {
				if (Tools.tl.get() == null) {
					Tools.tl.set(new Date());
				}
				System.out.println("B " + Tools.tl.get().getTime());
				Thread.sleep(100);
			}
		} catch (InterruptedException e) { 
			e.printStackTrace();
		}
	}
}

class ThreadA extends Thread {
	@Override
	public void run() {
		try {
			for (int i = 0; i < 20; i++) {
				if (Tools.tl.get() == null) {
					Tools.tl.set(new Date());
				}
				System.out.println("A " + Tools.tl.get().getTime());
				Thread.sleep(100);
			}
		} catch (InterruptedException e) { 
			e.printStackTrace();
		}
	}
}


class Tools {
	public static ThreadLocal<Date> tl = new ThreadLocal<Date>();
}
